package com.glorysoft.xztpda.bindingadapter

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.DataBindBaseViewHolder
import com.glorysoft.xztpda.databinding.ItemScrapInfoListBinding
import com.glorysoft.xztpda.entity.ScrapEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils
import java.lang.NumberFormatException

/**
 * FileName: ScrapInfoListAdapter
 * Author: XJH
 * Date: 2021/5/7 16:49
 * Description:
 * History:
 */
class ScrapInfoListAdapter(data: List<ScrapEntity>?) :
    BaseQuickAdapter<ScrapEntity, DataBindBaseViewHolder>
        (R.layout.item_scrap_info_list, data) {
    //    var num = -1
//    var isCheck = false
    var textView: TextView? = null
    override fun convert(helper: DataBindBaseViewHolder, item: ScrapEntity) {
        val itemScrapInfoListBinding: ItemScrapInfoListBinding =
            helper.getDataBinding() as ItemScrapInfoListBinding
        itemScrapInfoListBinding.scrapInfo = item
        itemScrapInfoListBinding.etNum.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(e: Editable?) {
                calculation()
            }

        })
        itemScrapInfoListBinding.executePendingBindings()
    }

    private fun calculation() {
        var tempNum = 0
        data.forEach {
            if (it.mainqty.isNullOrEmpty()) {
                it.mainqty = "0"
            }
            tempNum += it.mainqty.toInt()
        }

//        if (!isCheck) {
        textView?.text = tempNum.toString()
//            return
//        }

//        if (tempNum > num) {
//            val temp = num - (tempNum - i)
//            etNum.setText(temp.toString())
//            FreeApi_ToastUtils.showFreeToast("剩余最大数量$temp", mContext, false, 0)
//        }
    }


}